//
//  OrderDetailCCViewCell.m
//  DMGME
//
//  Created by MC on 15/10/9.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "OrderDetailCCViewCell.h"

#import "DMOrderDetailModel.h"

#import "UIImageView+WebCache.h"

#import "OrderDetaiABlViewCell.h"

#import "SubOrederdetailMode.h"

@implementation OrderDetailCCViewCell
{
    DMOrderDetailModel *mModel;
    NSString *str;
    NSString *t;
    NSString *h;
    NSString *fen;
    NSString *miao;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //        self.contentView.backgroundColor = [UIColor greenColor];
        // Initialization code
        UILabel *willPickup = [[UILabel alloc] initWithFrame:CGRectMake(2, 1, 159, 21)];
        _willPickup = willPickup;
        willPickup.font = [UIFont systemFontOfSize:12];
        willPickup.textColor = [UIColor redColor];
        [self.contentView addSubview:willPickup];
        
        UILabel *paymentStatus = [[UILabel alloc] initWithFrame:CGRectMake(rects.size.width-55, 1, 70, 21)];
        _paymentStatus = paymentStatus;
        paymentStatus.textColor = [UIColor colorWithRed:45/255.0 green:49/255.0 blue:51/255.0 alpha:1];
        paymentStatus.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:paymentStatus];
        
        UIImageView *linetop = [[UIImageView alloc] initWithFrame:CGRectMake(0, paymentStatus.frame.size.height+1, Width, 0.5)];
        
        linetop.alpha = 0.4;
        
        linetop.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:linetop];
        
        UITableView *centerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 21, Width, 85) style:UITableViewStylePlain];
        
        centerTableView.delegate = self;
        centerTableView.dataSource = self;
        _centerTableView = centerTableView;
        [_centerTableView registerClass:[OrderDetaiABlViewCell class] forCellReuseIdentifier:@"OrderDetaiABlViewCell"];
        _centerTableView.scrollEnabled = NO;
        [self.contentView addSubview:centerTableView];
        UILabel *totalMoney = [[UILabel alloc] initWithFrame:CGRectMake(rects.size.width-80, 110+centerTableView.frame.size.height, 80, 21)];
        _totalMoney = totalMoney;
        totalMoney.font = [UIFont systemFontOfSize:14];
        totalMoney.textColor = [UIColor grayColor];
        
        [self.contentView addSubview:totalMoney];
        UILabel *get_finishtime = [[UILabel alloc] init];
        _get_finishtime = get_finishtime;
        get_finishtime.font = [UIFont systemFontOfSize:14];
        get_finishtime.textColor = [UIColor grayColor];
        
        [self.contentView addSubview:get_finishtime];
        
        UILabel *order_time = [[UILabel alloc] init];
        
        _order_time = order_time;
        order_time.font = [UIFont systemFontOfSize:14];
        order_time.textColor = [UIColor grayColor];
        [self.contentView addSubview:order_time];
        
        UILabel *order_sn = [[UILabel alloc] init];
        _order_sn = order_sn;
        order_sn.font = [UIFont systemFontOfSize:14];
        order_sn.textColor = [UIColor grayColor];
        [self.contentView addSubview:order_sn];
        UIImageView *line = [[UIImageView alloc] init];
        line.tag = 12;
        line.backgroundColor = [UIColor lightGrayColor];
        line.alpha = 0.4;
        [self.contentView addSubview:line];
        
        UIButton *centeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        [centeBtn setBackgroundImage:[UIImage imageNamed:@"contactButton"] forState:UIControlStateNormal];
        _centeBtn = centeBtn;
        
        UIImageView *lineend = [[UIImageView alloc] init];
        lineend.tag = 13;
        lineend.backgroundColor = [UIColor lightGrayColor];
        lineend.alpha = 0.4;
        [self.contentView addSubview:lineend];
        [self.contentView addSubview:centeBtn];
        
        
        //        UIImageView *endLine = [[UIImageView alloc] init];
        //        _endLine = endLine;
        //        endLine.backgroundColor = [UIColor lightGrayColor];
        //        endLine.alpha = 0.4;
        //        endLine.tag = 15;
        //        [self.contentView addSubview:endLine];
    }
    return self;
}
- (void)awakeFromNib {
    
}

#pragma mark - tableViewMethod
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CGRect rect = CGRectMake(0, 26, [[UIScreen mainScreen]bounds].size.width,85*self.dmModel.goods.count);
    
    _centerTableView.frame = rect;
    _centeBtn.frame = CGRectMake(Width-75, 105+CGRectGetMaxY(_centerTableView.frame), 70, 30);
    _totalMoney.frame = CGRectMake(rects.size.width-80, CGRectGetMaxY(_centerTableView.frame)+5, 80, 21);
    _get_finishtime.frame = CGRectMake(15, 40+CGRectGetMaxY(_centerTableView.frame), 230, 21);
    _order_time.frame = CGRectMake(15, 60+CGRectGetMaxY(_centerTableView.frame), 260, 21);
    _order_sn.frame = CGRectMake(15, 80+CGRectGetMaxY(_centerTableView.frame), 175, 21);
    
    _centerTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    UIImageView *linetop = (UIImageView *)[self.contentView viewWithTag:13];
    linetop.frame = CGRectMake(0, 10+CGRectGetMaxY(_centerTableView.frame)+20, Width, 0.5);
    UIImageView *lineend = (UIImageView *)[self.contentView viewWithTag:14];
    lineend.frame = CGRectMake(0, self.frame.size.height-15, Width, 0.5);
    
    return self.dmModel.goods.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"OrderDetaiABlViewCell";
    
    
    OrderDetaiABlViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    
    cell.subModel = [SubOrederdetailMode modelWithDic:_dmModel.goods[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    _rectzm = CGRectGetMaxY(cell.numLabel.frame);
    
    //    _centerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    CGFloat height = CGRectGetMaxY(_order_sn.frame) ;
    
    return  85;
}
+(OrderDetailCCViewCell *)cellWithTableView:(UITableView *)tableView
{
    static  NSString*ident=@"cell";
    OrderDetailCCViewCell *cell=[tableView  dequeueReusableCellWithIdentifier:ident];
    
    if (!cell) {
        cell=[[OrderDetailCCViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ident];
        cell.contentView.backgroundColor = [UIColor redColor];
        
    }
    cell.contentView.backgroundColor = [UIColor clearColor];
    return cell;
}


-(void)setDmModel:(DMOrderDetailModel *)dmModel
{
    _dmModel = dmModel;
    
    mModel = dmModel;
    
    if(dmModel.status.intValue == 4 )
    {
        _willPickup.text = [NSString stringWithFormat:@"订单已完成"];
    }
    if(dmModel.status.intValue == 3 )
    {
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    [_willPickup setText:[NSString stringWithFormat:@"等待用户确认:"]];
    }
    _paymentStatus.text = @"在线支付";
    _get_finishtime.text = [NSString stringWithFormat:@"订单生成时间:%@",[self timeStamaps:dmModel.order_time]];
    NSString *string1 = [[self timeStamaps:dmModel.get_starttime] substringToIndex:16];
    NSString *string2 = [[self timeStamaps:dmModel.get_finishtime] substringWithRange:NSMakeRange(11, 5)];
    _order_time.text = [NSString stringWithFormat:@"取货时间:%@",[NSString stringWithFormat:@"%@~%@",string1,string2]];
    _order_sn.text = [NSString stringWithFormat:@"订单号:%@",dmModel.order_sn];
    float fl = dmModel.all_total.floatValue + dmModel.coupon.floatValue;
    _totalMoney.text = [NSString stringWithFormat:@"￥%0.2f",fl];
    
    [_centerTableView reloadData];
    
    
}

-(NSString *)timeStamaps:(NSString *)strs
{
    //时间戳转化为时间字符串
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[strs longLongValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
    NSString *string = [formatter stringFromDate:date];
    //    NSLog(@"date1:%@",str);
    return string;
}

//
- (void)timerFireMethod:(NSTimer *)theTimer
{
    BOOL timeStart = YES;
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    NSDateComponents *endTime = [[NSDateComponents alloc] init];    //初始化目标时间...
    NSDate *today = [NSDate date];    //得到当前时间
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateString = [dateFormatter dateFromString:str];
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:mModel.get_finishtime.longLongValue+432000];
    //    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *overdate = [dateFormatter stringFromDate:dateString];
    NSLog(@"overdate=%@",dateString);
    static int year;
    static int month;
    static int day;
    static int hour;
    static int minute;
    static int second;
    if(timeStart) {//从NSDate中取出年月日，时分秒，但是只能取一次
        year = [[overdate substringWithRange:NSMakeRange(0, 4)] intValue];
        month = [[overdate substringWithRange:NSMakeRange(5, 2)] intValue];
        day = [[overdate substringWithRange:NSMakeRange(8, 2)] intValue];
        hour = [[overdate substringWithRange:NSMakeRange(11, 2)] intValue];
        minute = [[overdate substringWithRange:NSMakeRange(14, 2)] intValue];
        second = [[overdate substringWithRange:NSMakeRange(17, 2)] intValue];
        timeStart= NO;
    }
    
    [endTime setYear:year];
    [endTime setMonth:month];
    [endTime setDay:day];
    [endTime setHour:hour];
    [endTime setMinute:minute];
    [endTime setSecond:second];
    //    NSDate *overTime = [cal dateFromComponents:endTime]; //把目标时间装载入date
    //用来得到具体的时差，是为了统一成北京时间
    unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:unitFlags fromDate:today  toDate:date options:0];
    t = [NSString stringWithFormat:@"%ld", (long)[d day]];
    h = [NSString stringWithFormat:@"%ld", (long)[d hour]];
    fen = [NSString stringWithFormat:@"%ld", (long)[d minute]];
    if([d minute] < 10) {
        fen = [NSString stringWithFormat:@"0%ld",(long)[d minute]];
    }
    miao = [NSString stringWithFormat:@"%ld", (long)[d second]];
    if([d second] < 10) {
        miao = [NSString stringWithFormat:@"0%ld",(long)[d second]];
    }
    if(mModel.status.intValue == 3 )
    {
    [_willPickup setText:[NSString stringWithFormat:@"等待用户确认:%@天 %@:%@:%@",t,h,fen,miao]];
    }
    if([d second] > 0) {
        //计时尚未结束，do_something
        //        [_longtime setText:[NSString stringWithFormat:@"%@:%@:%@",d,fen,miao]];
    } else if([d second] == 0) {
        //计时结束 do_something
        
    } else{
        //计时器失效
        [theTimer invalidate];
    }
    
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

